<?php
use think\Db;

/**
 * 记录日志
 * @param  [type] $uid         [用户id]
 * @param  [type] $username    [用户名]
 * @param  [type] $description [描述]
 * @param  [type] $status      [状态]
 * @return [type]              [description]
 * @return [type] $type        0正确  -1错误日志
 */
function writelog($description,$type=0,$user=array('id'=>0,'username'=>'默认'))
{
    $data['admin_id'] = $user['id'];
    $data['admin_name'] = $user['username'];
    $data['description'] = $description;
    $data['status'] = $type<0 ? -1: 1;
    $data['ip'] = request()->ip();
    $data['create_time'] = time();
    Db::name('log_admin')->insert($data);
}

function getOpenid($appid)
{
    $SERVER_NAME = $_SERVER['SERVER_NAME'];
    $REQUEST_URI = $_SERVER['REQUEST_URI'];
    $redirect_uri = urlencode('http://' . $SERVER_NAME . $REQUEST_URI);

    // 网页授权当scope=snsapi_userinfo时才会提示是否授权应用
    $autourl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
    $row = posturl($autourl);
    return ($row['openid']);
}
function posturl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $jsoninfo = json_decode($output, true);
    return $jsoninfo;
}

/**
 * 替换屏蔽词内容
 * @param  integer $len 长度
 * @return string
 */
function ping($content) {
    $result = Db::name('ping')->field('string')->select();
    $result = array_column($result,'string','a');
    $pre = '/'.implode('|',$result).'/';
    $content = preg_replace($pre, "*", $content);
    return $content;
}

/**
 * 修改图片记录
 * @param
 * @return string
 */
function img_log($data)
{
    db('log_img')->insert($data);
}

/**
 * 消息时间提示
 * @param  integer $len 长度
 * @return string
 */
function MessageTime($time)
{
    //获取今天凌晨的时间戳
    $day = strtotime(date('Y-m-d',time()));
    //获取昨天凌晨的时间戳
    $pday = strtotime(date('Y-m-d',strtotime('-1 day')));
    //获取现在的时间戳
    $nowtime = time();

    $tc = $nowtime-$time;
    if($time<$pday){
        $str = date('Y-m-d',$time);
    }elseif($time<$day && $time>$pday){
        $str = "昨天";
    }elseif($tc>60*60){
        $str = floor($tc/(60*60))."小时前";
    }elseif($tc>60){
        $str = floor($tc/60)."分钟前";
    }else{
        $str = "刚刚";
    }
    return $str;
}

function time2string($second){
    $day = ceil($second/(3600*24));
    $second = $second%(3600*24);//除去整天之后剩余的时间
    $hour = floor($second/3600);
    $second = $second%3600;//除去整小时之后剩余的时间
    $minute = floor($second/60);
    $second = $second%60;//除去整分钟之后剩余的时间
    if($day != 0){
        $data = $day;
    }elseif($day == 0 && $hour != 0){
        $data = 1;
    }elseif($day == 0 && $hour == 0 && $minute != 0){
        $data = 1;
    }elseif ($day == 0 && $hour == 0 && $minute == 0 && $second != 0){
        $data = 1;
    }
    //返回字符串
    return $data;
}

